﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    /// <summary>
    /// 攀藤PMS5003T 串口数据模型
    /// </summary>
    public class DeviceDataModel
    {
        /// <summary>
        /// 表示PM1.0浓度（CF=1，标准颗粒物）单位μg/m3
        /// </summary>
        public int PM1Standard { get; private set; }

        /// <summary>
        /// PM2.5浓度（CF=1，标准颗粒物）单位μg/m3
        /// </summary>
        public int PM2Standard { get; private set; }

        /// <summary>
        /// PM10浓度（CF=1，标准颗粒物）单位μg/m3
        /// </summary>
        public int PM10Standard { get; private set; }

        /// <summary>
        /// PM1.0浓度（大气环境下）单位μg/m3
        /// </summary>
        public int PM1Atmosphere { get; private set; }

        /// <summary>
        /// PM2.5浓度（大气环境下）单位μg/m3
        /// </summary>
        public int PM2Atmosphere { get; private set; }

        /// <summary>
        /// PM10浓度（大气环境下）单位μg/m3
        /// </summary>
        public int PM10Atmosphere { get; private set; }

        /// <summary>
        /// 0.1升空气中直径在0.3um以上颗粒物个数
        /// </summary>
        public int PM3Count { get; private set; }

        /// <summary>
        /// 0.1升空气中直径在0.5um以上颗粒物个数
        /// </summary>
        public int PM5Count { get; private set; }

        /// <summary>
        /// 0.1升空气中直径在1.0um以上颗粒物个数
        /// </summary>
        public int PM10Count { get; private set; }

        /// <summary>
        /// 0.1升空气中直径在2.5um以上颗粒物个数
        /// </summary>
        public int PM25Count { get; private set; }

        /// <summary>
        /// 温度
        /// </summary>
        public double Temperature { get; private set; }

        /// <summary>
        /// 湿度
        /// </summary>
        public double Humidity { get; private set; }

        /// <summary>
        /// 版本号
        /// </summary>
        public int Version { get; private set; }

        /// <summary>
        /// 错误代码
        /// </summary>
        public int ErrorCode { get; private set; }

        /// <summary>
        /// 串口数据
        /// </summary>
        public DeviceDataModel Calc(string data)
        {
            try
            {
                //42 4D 00 1C 00 6D 00 8E 00 9A 00 48 00 5E 00 66 43 41 14 A9 02 EE 00 34 00 EE 00 BE 91 00 07 EE 

                string[] ds = data.Split(' ');
                if (ds.Length != 33)
                {
                    return null;
                }

                int checkA = 0;
                int num = 0;
                for (int i = 0; i < 30; i++)
                {
                    checkA += int.Parse(ds[i], NumberStyles.HexNumber);
                    num++;
                }

                if (int.Parse(ds[30] + ds[31], NumberStyles.HexNumber) != checkA)
                {
                    return null;
                }

                PM1Standard = int.Parse(ds[4] + ds[5], NumberStyles.HexNumber);
                PM2Standard = int.Parse(ds[6] + ds[7], NumberStyles.HexNumber);
                PM10Standard = int.Parse(ds[8] + ds[9], NumberStyles.HexNumber);
                PM1Atmosphere = int.Parse(ds[10] + ds[11], NumberStyles.HexNumber);
                PM2Atmosphere = int.Parse(ds[12] + ds[13], NumberStyles.HexNumber);
                PM10Atmosphere = int.Parse(ds[14] + ds[15], NumberStyles.HexNumber);
                PM3Count = int.Parse(ds[16] + ds[17], NumberStyles.HexNumber);
                PM5Count = int.Parse(ds[18] + ds[19], NumberStyles.HexNumber);
                PM10Count = int.Parse(ds[20] + ds[21], NumberStyles.HexNumber);
                PM25Count = int.Parse(ds[22] + ds[23], NumberStyles.HexNumber);
                Temperature = int.Parse(ds[24] + ds[25], NumberStyles.HexNumber) / 10.0;
                Humidity = int.Parse(ds[26] + ds[27], NumberStyles.HexNumber) / 10.0;
                Version = int.Parse(ds[28], NumberStyles.HexNumber);
                ErrorCode = int.Parse(ds[29], NumberStyles.HexNumber);

                return this;
            }
            catch
            {
                throw;
            }
        }
    }
}
